package oo

import (
	"elegantGo/chapter03/pkg/validate"
	"time"
)

type Sort string

func (o *Sort) OrDefault() string {
	if o != nil {
		return string(*o)
	}
	return "CreateTime"
}

func (o *Sort) IsValid(v string) bool {
	return validate.Var(v, "alphanum") == nil
}

type Order string

func (o *Order) OrDefault() string {
	if o != nil {
		return string(*o)
	}
	return "DESC"
}

func (o *Order) IsValid(v string) bool {
	return validate.Var(v, "oneof=DESC ASC") == nil
}

type Offset int

func (o *Offset) OrDefault() int {
	if o != nil {
		return int(*o)
	}
	return 0
}

func (o *Offset) IsValid(v int64) bool {
	return validate.Var(v, "min=0") == nil
}

type Limit int

func (o *Limit) OrDefault() int {
	if o != nil {
		return int(*o)
	}
	return 20
}

func (o *Limit) IsValid(v int64) bool {
	return validate.Var(v, "min=20,max=100") == nil
}

type IntRange struct {
	Min *int
	Max *int `binding:"omitempty,isdefaultfield=Min|gtfield=Min"`
}

type FloatRange struct {
	Min *float64
	Max *float64 `binding:"omitempty,isdefaultfield=Min|gtfield=Min"`
}

type TimeRange struct {
	Min *time.Time
	Max *time.Time `binding:"omitempty,isdefaultfield=Min|gtfield=Min"`
}
